package com.chen.controller;

import com.chen.entity.Student;
import com.chen.entity.vo.Result;
import com.chen.entity.vo.ResultCodeEnum;
import com.chen.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 条件分页查询
     */
    @GetMapping("/selectByCondition/{pageNum}/{pageSize}")
    public Result<PageInfo<Student>> selectByCondition(Student studentDto,
                                              @PathVariable Integer pageNum,
                                              @PathVariable Integer pageSize){
        PageInfo<Student>  pageInfo = studentService.selectByCondition(studentDto, pageNum, pageSize);
        return Result.build(pageInfo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Student> students = studentService.selectAll();
        return Result.build(students, ResultCodeEnum.SUCCESS);
    }

    /**
     * 新增学生方法
     *
     * @param student
     * @return
     */
    @PostMapping("/add")
    public Result addStudent(@RequestBody Student student) {
        studentService.addStudent(student);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        studentService.deleteById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id修改
     *
     * @param student
     * @return
     */
    @PutMapping("/edit")
    public Result editById(@RequestBody Student student) {
        studentService.editById(student);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


}
